/*
 * SPI.c
 *
 *  Created on: Jan 20, 2012
 *      Author: Administrator
 */
#include "SPI.h"

void initSPI()
{
	//MOSI SCK and SS outputs
	SPI_DDR |= MOSI_bm | SCK_bm | SS_bm;
	//MISO input
	SPI_DDR &= ~MISO_bm;
	//Mode 1, fOSC/4
	SPCR = _BV(SPE)|_BV(MSTR)|_BV(CPHA);
}

inline uint8_t readSPI()
{
	while (!(SPSR & _BV(SPIF)))
	{};
	return SPDR;
}

inline void writeSPI(uint8_t data)
{
	SPDR = data;
	while (!(SPSR & _BV(SPIF)));
}

inline uint8_t transferSPI(uint8_t data)
{
	SPDR = data;
	while (!(SPSR & _BV(SPIF)))
	{};
	return SPDR;
}


